<?php
/*
 * This is a smarty function
 * Use to build list of category to Menuitem format
 * in smarty
 */
 
function build_menu_item($params) 
{ 
	if(empty($params['cats']))
	{ 
	   return ""; 
	} 
	else 
	{ 
	   $cats = $params['cats'];
	   $str = '<ul>';
	   $str .= build_li($cats);
	   $str .= '</ul>'; 
	   
	   return $str;
	} 
}

function build_li($array)
{
	$str = "";
	foreach($array as $cat)
	{
	if($cat->cat_ID == $_SESSION["cat"])		
		{	
		$str .= sprintf('<li id="%s" class="active">' .
				'<a {_link_} title="%s">' .
				'<span class="textmenu">%s</span>' .
				'</a>',
				$cat->cat_ID,
				$cat->cat_description,
				$cat->cat_name);
		}
	else{
		$str .= sprintf('<li id="%s">' .
				'<a {_link_} title="%s">' .
				'<span class="textmenu">%s</span>' .
				'</a>',
				$cat->cat_ID,
				$cat->cat_description,
				$cat->cat_name);
	}
	
	
	
		
		
		if($cat->sub != null)
		{
			//TODO: wrap this line if you don't want popup have link
			//$str = str_replace("{_link_}", "", $str);
			$str = str_replace("{_link_}", sprintf('href="%s"', build_link($cat)), $str);
			
			$str .= '<ul width="200">';
			$str .= build_li($cat->sub);
			$str .= "</ul>";
		}
		else
		{
			$str = str_replace("{_link_}", sprintf('href="%s"', build_link($cat)), $str);
		}
		
		$str .= "</li>";
	}
	
	return $str;
}

function build_link($cat)
{
	//TODO: build the link of menu item base on actual website structure
	return "?cat=" . $cat->cat_ID ;
}

$smarty->register_function("buildmenu", "build_menu_item"); 
?>
